import htmlDocx from 'html-docx-js/dist/html-docx';
import { saveAs } from 'file-saver';

export const exportAsDocx = (htmlContent, fileName = 'document.docx') => {
  // 1. 处理样式兼容性（Word 不支持部分 CSS）
  const processedHTML = `
    <html>
      <head>
        <style>
          body { font-family: Arial, sans-serif; margin: 20px; }
          table { border-collapse: collapse; width: 100%; }
          td, th { border: 1px solid #ddd; padding: 8px; }
          img { max-width: 500px; height: auto; }
        </style>
      </head>
      <body>
        ${htmlContent}
      </body>
    </html>
  `;

  // 2. 转换 HTML 为 Word 支持的 Blob
  const docxBlob = htmlDocx.asBlob(processedHTML);

  // 3. 触发下载
  saveAs(docxBlob, fileName);
};